package com.it;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * @Author：Dream
 * @Package：com.it
 * @Project：demo
 * @name：ShiroRun
 * @Date：2025/10/21 17:12
 * @Filename：ShiroRun
 */
public class ShiroRun {

    public static void main(String[] args) {
        IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);

        Subject subject = SecurityUtils.getSubject();

        AuthenticationToken token = new UsernamePasswordToken("zhangsan", "z3");

        try {
            subject.login(token);
            System.out.println("登录成功");
            System.out.println(subject.hasRole("role1"));
            System.out.println(subject.isPermitted(("user:insert")));

        } catch (AuthenticationException e) {
            throw new RuntimeException(e);
        }
    }
}
